var t = getApp(), e = require("../confirm/tmp.js");

Page({
    data: {
        checkAll: !1,
        total: 0,
        isMana: !1,
        num: 3,
        server: t.server,
        serverUrl: t.serverUrl
    },
    mana: function() {
        this.setData({
            isMana: !this.data.isMana
        });
    },
    rec: function(t) {
        this.cal(t, -1);
    },
    add: function(t) {
        this.cal(t, 1);
    },
    cal: function(e, a) {
        var r = this.data.products[e.currentTarget.dataset.index].quality;
        if (0 !== (r += a)) {
            var o = {};
            o["products[" + e.currentTarget.dataset.index + "].quality"] = r, this.setData(o), 
            this.calTotal(), t.request({
                url: t.server + "/orderApi/modifyUserCart",
                method: "POST",
                data: {
                    token: t.token,
                    userToken: this.data.usr.userToken,
                    pc_id: this.data.products[e.currentTarget.dataset.index].id,
                    quality: r
                },
                header: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                success: function(t) {
                    0 !== t.data.code && wx.showToast({
                        title: t.data.msg.zh,
                        icon: "none"
                    });
                }
            });
        }
    },
    calTotal: function() {
        if (this.data.products) {
            var t = this.data.products.filter(function(t) {
                return t.checked;
            }).map(function(t) {
                return t.quality * t.price;
            });
            this.setData({
                total: t.length > 0 ? t.reduce(function(t, e) {
                    return t + e;
                }) : 0
            });
        } else this.setData({
            total: 0
        });
    },
    del: function(e) {
        var a = this.data.products[e.currentTarget.dataset.index].id;
        this.data.products.splice(e.currentTarget.dataset.index, 1), this.setData({
            products: this.data.products
        }), t.request({
            url: t.server + "/orderApi/delUserCart",
            method: "POST",
            data: {
                token: t.token,
                userToken: this.data.usr.userToken,
                pc_id: a
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                0 !== t.data.code && wx.showToast({
                    title: t.data.msg.zh,
                    icon: "none"
                });
            }
        });
    },
    checkallToggle: function(t) {
        var e = !this.data.checkAll, a = {
            checkAll: e
        };
        if (this.data.products) {
            for (var r = 0; r < this.data.products.length; r++) a["products[" + r + "].checked"] = e;
            this.setData(a), this.calTotal();
        }
    },
    checkToggle: function(t) {
        var e = this.data.products[t.currentTarget.dataset.index].checked, a = {};
        a["products[" + t.currentTarget.dataset.index + "].checked"] = !e, this.setData(a), 
        this.calTotal();
    },
    onLoad: function(t) {},
    reload: function() {
        var e = this, a = wx.getStorageSync("usr");
        if (this.setData({
            usr: a
        }), !a) return this.setData({
            products: null
        }), void this.calTotal();
        t.request({
            url: t.server + "/orderApi/getUserCart",
            method: "POST",
            data: {
                token: t.token,
                userToken: a.userToken
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var a = [];
                if (0 === t.data.code) {
                    var r = !0, o = !1, n = void 0;
                    try {
                        for (var s, c = t.data.result.products[Symbol.iterator](); !(r = (s = c.next()).done); r = !0) {
                            var i = s.value;
                            i.quality = Number(i.quality), i.price = Number(i.price), i.checked = !1;
                        }
                    } catch (t) {
                        o = !0, n = t;
                    } finally {
                        try {
                            !r && c.return && c.return();
                        } finally {
                            if (o) throw n;
                        }
                    }
                    a = t.data.result.products;
                }
                e.setData({
                    products: a
                }), e.calTotal();
            }
        }), t.request({
            url: t.server + "/userApi/getFactoryInfo",
            method: "POST",
            data: {
                token: t.token
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                e.setData({
                    vendor: t.data.result.info
                });
            }
        });
    },
    settle: function() {
        var t = null;
        this.data.products && (t = this.data.products.filter(function(t) {
            return t.checked;
        })), t && 0 !== t.length ? (e.set({
            products: t,
            vendor: this.data.vendor,
            total: this.data.total
        }), wx.navigateTo({
            url: "/pages/order/confirm/confirm"
        })) : wx.showToast({
            icon: "none",
            title: "请选择需要结算的产品"
        });
    },
    onReady: function() {},
    onShow: function() {
        this.reload();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});